package cn.tedu.springdemo.bean;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class DemoAspect {

    private static Logger logger =
            LoggerFactory.getLogger(DemoAspect.class);

    @Before("bean(demoBean)")
    public void test(){
        System.out.println("DemoAspect.test");
    }

    @Around("bean(demoBean)")
    public Object aroundTest(ProceedingJoinPoint joinPoint)
        throws  Throwable{
        Signature signature = joinPoint.getSignature();
        //方法之前
        long t1 = System.nanoTime();
        //ProceedingJoinPoint 处理过程连接点
        //proceed() 就是执行目标对象的方法 method.invoke()
        Object value = joinPoint.proceed();
        //方法之后
        // Signature 就是方法签名
        long t2 = System.nanoTime();
        long time = t2-t1;
        //System.out.println("方法:"+signature+" 执行时间:"+time);
        logger.debug("方法 {} 执行时间 {}", signature, time);
        return value;
    }

}
